package com.weilive.platform.gate.json.handler;

import org.springframework.stereotype.Service;

import com.weilive.core.netty.NettyErrors;
import com.weilive.core.protocol.json.JsonRequest;
import com.weilive.core.protocol.json.JsonRequestHandler;
import com.weilive.core.protocol.json.JsonResponse;
import com.weilive.core.utils.RSAUtils;
import com.weilive.core.utils.RegexUtils;
import com.weilive.platform.PlatformErrors;
import com.weilive.platform.PlatformSecurity;
import com.weilive.platform.cache.app.AppManager;
import com.weilive.platform.cache.user.UserManager;
import com.weilive.platform.cache.user.UserObj;
import com.weilive.platform.gate.json.protocol.CSA_PlatUserRegister;
import com.weilive.platform.gate.json.protocol.CSQ_PlatUserRegister;

/**
 * 
 * 用户注册
 * 
 * @author houge
 *
 */
@Service
public class CSQ_PlatUserRegister_JsonHandler extends JsonRequestHandler {

	public Class<?> getRequestType() {
		return CSQ_PlatUserRegister.class;
	}

	public Class<?> getResponseType() {
		return CSA_PlatUserRegister.class;
	}

	public JsonResponse handleRequest(JsonRequest request) throws Exception {
		CSQ_PlatUserRegister params = (CSQ_PlatUserRegister) request.getData();

		// 读取客户端公钥
		String pub_key = "";
		if (params.pub_key != null && params.pub_key.size() == 2) {
			for (String key : params.pub_key) {
				String data = PlatformSecurity.base64ServerDecrypt(key);
				if (data != null) {
					pub_key += data;
				} else {
					pub_key = null;
					break;
				}
			}
		}
		if (pub_key == null || params.app_id == null) {
			return createErrorResponse(NettyErrors.BAD_REQUEST);
		}
		// 游戏检测
		if (AppManager.getApp(params.app_id) == null) {
			return createErrorResponse(PlatformErrors.APP_NOT_EXIST);
		}

		if ((params.account != null || params.email != null || params.phone != null) && params.password != null
				&& params.pub_key != null) {
			// 账号检测
			String password = null;
			try {
				password = RSAUtils.base64DecryptByPublicKey(params.password, pub_key);
				if (password == null || RegexUtils.isPassword(password) == false) {
					return createErrorResponse(PlatformErrors.INVALID_PASSWORD);
				}
			} catch (Exception e) {
				return createErrorResponse(NettyErrors.BAD_REQUEST);
			}
			if (params.account != null && RegexUtils.isAccount(params.account) == false) {
				return createErrorResponse(PlatformErrors.INVALID_ACCOUNT);
			}
			if (params.email != null && RegexUtils.isEmail(params.email) == false) {
				return createErrorResponse(PlatformErrors.INVALID_EMAIL);
			}
			if (params.phone != null && RegexUtils.isPhone(params.phone) == false) {
				return createErrorResponse(PlatformErrors.INVALID_PHONE);
			}
			UserObj user = UserManager.createUser("g" + params.app_id, params.account, params.email, params.phone,
					password);
			if (user == null) {
				return createErrorResponse(PlatformErrors.USER_ALREADY_EXIST);
			}

			// 返回结果
			CSA_PlatUserRegister response = new CSA_PlatUserRegister();
			response.uin = UserManager.getUin(user.info.user_id);
			response.token = user.updateToken(params.app_id);
			response.date = System.currentTimeMillis();
			response.account = user.info.account;
			response.email = user.info.email;
			response.phone = user.info.phone;
			try {
				response.secret = RSAUtils.base64EncryptByPublicKey(user.info.secret, pub_key);
			} catch (Exception e) {
				return createErrorResponse(NettyErrors.BAD_REQUEST);
			}
			return JsonResponse.create(response);
		} else {
			return createErrorResponse(NettyErrors.BAD_REQUEST);
		}
	}

}
